package sam.learn.biz.datasource.repo.entity;

import java.util.HashMap;
import java.util.Map;

public enum DatasourceBrandEnum {

    MARIADB("mariadb", "org.mariadb.jdbc.Driver", "jdbc:mariadb://%s:%s/%s?characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&useSSL=false"),
    MYSQL5("mysql5", "com.mysql.cj.jdbc.Driver", "jdbc:mysql://%s:%s/%s?characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&useSSL=false"),
    MYSQL8("mysql8", "com.mysql.cj.jdbc.Driver", "jdbc:mysql://%s:%s/%s?characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&useSSL=false"),
    ERROR("error", "errorDriveClass", "errorUrl: %s,%s,%s");


    private final String brandCode;
    private final String driveClassName;
    private final String urlTemplate;

    public String getBrandCode() {
        return brandCode;
    }

    public String getDriveClassName() {
        return driveClassName;
    }

    public String getUrlTemplate() {
        return urlTemplate;
    }

    public String getUrl(String ip, String port, String schemaName) {
        return String.format(getUrlTemplate(), ip, port, schemaName);
    }


    DatasourceBrandEnum(String brandCode, String driveClassName, String urlTemplate) {
        this.brandCode = brandCode;
        this.driveClassName = driveClassName;
        this.urlTemplate = urlTemplate;
    }

    public static final Map<String, DatasourceBrandEnum> keyEnumMap;

    static {
        DatasourceBrandEnum[] values = DatasourceBrandEnum.values();
        keyEnumMap = new HashMap<>(values.length);
        for (DatasourceBrandEnum anEnum : values) {
            keyEnumMap.put(anEnum.getBrandCode(), anEnum);
        }
    }


    public static DatasourceBrandEnum getDatasourceBrandEnumByKey(String key) {
        return keyEnumMap.getOrDefault(key, ERROR);
    }
}
